﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;

namespace BasisMgr
{
    public partial class TreeComBoxEdit : UserControl
    {
        public string FieldID = "ID";
        public string FieldName = "Name";
        public delegate void PopUp(object sender, CancelEventArgs e);
        public event PopUp OnPopUp;


        public string ID_Tag
        {
            get { return GetIDTag(); }
            set
            {
                SetIDTag(value);
            }
        }

        public string ID_EditValue
        {
            get { return GetIDEditValue(); }
        }

        public DevExpress.XtraTreeList.TreeList treeList = new DevExpress.XtraTreeList.TreeList();
       
        public TreeComBoxEdit()
        {
            InitializeComponent();            
        }
        private void TreeComBoxEdit_Load(object sender, EventArgs e)
        {
            popupCtlDept.Width = 400;
            popupCtlDept.Height = 400;
            treeList = tlDept;
        }

        public void txtID_QueryPopUp(object sender, CancelEventArgs e)
        {
            try
            {
                OnPopUp(sender, e);
            }
            catch (Exception)
            {
            }
           
            PopupContainerEdit popupedit1 = (PopupContainerEdit)sender;
            popupCtlDept.Width = popupedit1.Width;

            for (int i = 0; i < tlDept.Nodes.Count; i++)
            {
                TreeListNode node = tlDept.Nodes[i];
                SetFocusedNode(node);
            }
 
        }

        private void SetFocusedNode(TreeListNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeListNode newNode in node.Nodes)
                {
                    if (newNode[FieldID].ToString() == (string)txtID.Tag)
                    {
                        tlDept.SetFocusedNode(newNode);                        
                        return;
                    }
                    SetFocusedNode(newNode);
                }
            }
        }


        private void SetIDTag(string sTag)
        {
            if ((sTag != null)&&(sTag != ""))
            {
                this.Tag = sTag;
                txtID.Tag = sTag;
                txtID.Text = "";
                tlDept.MoveFirst();
                //tlDept.MoveFirst();

               // string sFocusedNode = tlDept.FocusedNode["ID"].ToString();
                //if (sFocusedNode == sTag)
                //{
                //    txtID.EditValue = tlDept.FocusedNode[FieldName].ToString();
                //    return;
                //}

                for (int i = 0; i < tlDept.Nodes.Count; i++)
                {
                    TreeListNode node = tlDept.Nodes[i];
                    SetNodeTagChanged(node);
                }
            }
            else
            {
                this.Tag = "";
                txtID.Tag = "";
                txtID.Text = "";
            }
        }

        private string GetIDTag()
        {
            this.Tag = (string)txtID.Tag;
            if (txtID.Tag == null)
                return "";
            else
                return (string)txtID.Tag;

        }

        private string GetIDEditValue()
        {
            if (txtID.EditValue == null)
                return "";
            else
                return (string)txtID.EditValue;
        }

        private void SetNodeTagChanged(TreeListNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeListNode newNode in node.Nodes)
                {
                    string sNode = newNode[FieldID].ToString();
                    if (sNode == (string)txtID.Tag)
                    {
                        txtID.EditValue = newNode[FieldName].ToString();
                        return;
                    }
                    SetNodeTagChanged(newNode);
                }
            }
        }

        private void tlDept_Click(object sender, EventArgs e)
        {
            TreeListNode node = tlDept.FocusedNode;
            txtID.Tag = node[FieldID].ToString();
            txtID.EditValue = node[FieldName].ToString();
            txtID.ClosePopup();
        }

    }
}
